package handlers

import (
	"gin-quickstart/internal/models"
	"gin-quickstart/internal/modules/product/services"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// ProductHandler 产品处理器
type ProductHandler struct {
	productService services.ProductService
}

// NewProductHandler 创建产品处理器实例
func NewProductHandler(productService services.ProductService) *ProductHandler {
	return &ProductHandler{productService: productService}
}

// RegisterRoutes 注册产品相关路由
func (h *ProductHandler) RegisterRoutes(router *gin.RouterGroup) {
	products := router.Group("/products")
	{
		// SPU相关路由
		products.GET("", h.GetAllSPUs)
		products.GET("/:id", h.GetSPUByID)
		products.POST("", h.CreateSPU)
		products.PUT("/:id", h.UpdateSPU)
		products.DELETE("/:id", h.DeleteSPU)
		
		// SKU相关路由
		products.GET("/:id/skus", h.GetSKUsBySPUID)
		products.POST("/:id/skus", h.CreateSKU)
	}
}

// GetAllSPUs 获取所有SPU产品
func (h *ProductHandler) GetAllSPUs(c *gin.Context) {
	spus, err := h.productService.GetAllSPUs()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get products: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, spus)
}

// GetSPUByID 根据ID获取SPU产品
func (h *ProductHandler) GetSPUByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
		return
	}
	
	spu, err := h.productService.GetSPUByID(uint(id))
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get product: " + err.Error()})
		}
		return
	}
	c.JSON(http.StatusOK, spu)
}

// CreateSPU 创建SPU产品
func (h *ProductHandler) CreateSPU(c *gin.Context) {
	var spu models.SPU
	if err := c.ShouldBindJSON(&spu); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request data: " + err.Error()})
		return
	}
	
	if err := h.productService.CreateSPU(&spu); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create product: " + err.Error()})
		return
	}
	c.JSON(http.StatusCreated, spu)
}

// UpdateSPU 更新SPU产品
func (h *ProductHandler) UpdateSPU(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
		return
	}
	
	var spu models.SPU
	if err := c.ShouldBindJSON(&spu); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request data: " + err.Error()})
		return
	}
	
	spu.SPUID = uint(id)
	if err := h.productService.UpdateSPU(&spu); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update product: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, spu)
}

// DeleteSPU 删除SPU产品
func (h *ProductHandler) DeleteSPU(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
		return
	}
	
	if err := h.productService.DeleteSPU(uint(id)); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete product: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Product deleted successfully"})
}

// GetSKUsBySPUID 根据SPU ID获取所有SKU
func (h *ProductHandler) GetSKUsBySPUID(c *gin.Context) {
	spuID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
		return
	}
	
	skus, err := h.productService.GetSKUsBySPUID(uint(spuID))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get SKUs: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, skus)
}

// CreateSKU 创建SKU
func (h *ProductHandler) CreateSKU(c *gin.Context) {
	spuID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
		return
	}
	
	var sku models.SKU
	if err := c.ShouldBindJSON(&sku); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request data: " + err.Error()})
		return
	}
	
	sku.SPUID = uint(spuID)
	if err := h.productService.CreateSKU(&sku); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create SKU: " + err.Error()})
		return
	}
	c.JSON(http.StatusCreated, sku)
}
